package com.xunqi.gulimall.auth.myutil;//package com.xunqi.gulimall.product.myutil;
//
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Qualifier;
//import org.springframework.boot.CommandLineRunner;
//import org.springframework.stereotype.Component;
//import org.springframework.web.method.HandlerMethod;
//import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
//import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
//
//import java.util.Map;
//
//@Component
//public class UrlPrinter implements CommandLineRunner {
//
//    private final RequestMappingHandlerMapping requestMappingHandlerMapping;
//
//    @Autowired
//    public UrlPrinter(@Qualifier("requestMappingHandlerMapping") RequestMappingHandlerMapping requestMappingHandlerMapping) {
//        this.requestMappingHandlerMapping = requestMappingHandlerMapping;
//    }
//
//    @Override
//    public void run(String... args) throws Exception {
//        System.out.println("Listing all URLs:");
//        for (Map.Entry<RequestMappingInfo, HandlerMethod> entry : requestMappingHandlerMapping.getHandlerMethods().entrySet()) {
//            assert entry.getKey().getPatternsCondition() != null;
//            entry.getKey().getPathPatternsCondition().getPatterns().forEach(System.out::println);
//        }
//    }
//}


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TreeMap;
import java.util.stream.Collectors;

@Component
public class UrlPrinter implements CommandLineRunner {

    private final RequestMappingHandlerMapping requestMappingHandlerMapping;

    @Autowired
    public UrlPrinter(RequestMappingHandlerMapping requestMappingHandlerMapping) {
        this.requestMappingHandlerMapping = requestMappingHandlerMapping;
    }

    @Override
    public void run(String... args) throws Exception {
        // 获取所有请求映射
        Map<RequestMappingInfo, HandlerMethod> handlerMethods = requestMappingHandlerMapping.getHandlerMethods();

        // 根据控制器对 URL 进行分组
        Map<String, Map<String, String>> controllerUrls = new LinkedHashMap<>();
        for (Map.Entry<RequestMappingInfo, HandlerMethod> entry : handlerMethods.entrySet()) {
            HandlerMethod handlerMethod = entry.getValue();
            String controllerName = handlerMethod.getBeanType().getSimpleName();
            String methodName = handlerMethod.getMethod().getName();
            //String url = entry.getKey().getPatternsCondition().getPatterns().stream().collect(Collectors.joining(", "));
            String url = entry.getKey().getPathPatternsCondition().getPatterns().stream().map(r->r.getPatternString()).collect(Collectors.joining());

            controllerUrls.computeIfAbsent(controllerName, k -> new TreeMap<>()).put(methodName, url);
        }

        // 打印结果
        System.out.println("Listing all URLs by Controller:");
        controllerUrls.forEach((controller, urls) -> {
            System.out.println("\nController: " + controller);
            urls.forEach((method, url) -> System.out.println("  Method: " + method + ", URL(s): " + url));
        });
    }
}
